package com.dyz.leetcode;

import java.util.HashSet;

public class HasCycle141 {
    public boolean hasCycle(ListNode head) {
        if(head==null||head.next==null){return false;}
        HashSet<ListNode> set = new HashSet<>();
        while (head!=null){
            if(set.contains(head)){return true;}
            set.add(head);
            head =head.next;
        }
        return false;
    }

    //快慢指针
    public boolean hasCycle1(ListNode head) {
        if(head==null||head.next==null){return false;}
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast==slow){return true;}
        }
        return false;
    }

    public static void main(String[] args) {
        HashSet<Integer> set = new HashSet<>();
        System.out.println(set.add(1));
        System.out.println(set.add(1));
    }
}
